<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\balancediscount\model;

class UserCard extends \app\BaseModel
{
    protected $name = "massage_balance_discount_user_card";
    public function getCashAttr($value, $data)
    {
        if(isset($value)) {
            return round($value, 2);
        }
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("top desc,id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        return !empty($data) ? $data->toArray() : [];
    }
    public function dataAdd($order)
    {
        $insert = ["uniacid" => $order["uniacid"], "user_id" => $order["user_id"], "order_code" => longbingorderCodetf(), "card_order_id" => $order["id"], "cash" => $order["pay_price"], "total_cash" => $order["pay_price"], "discount" => $order["discount"], "over_time" => strtotime("+" . $order["month"] . " months"), "title" => $order["title"], "create_time" => time()];
        $res = $this->insert($insert);
        return $res;
    }
    public function getUserCard($user_id, $service_price, $car_price, $card_arr = [])
    {
        if(empty($card_arr)) {
            $dis = ["user_id" => $user_id, "status" => 1];
        } else {
            $dis[] = ["id", "in", $card_arr];
        }
        $card = $this->where($dis)->where("over_time", ">", time())->where("cash", ">", 0)->order("discount,cash,id desc")->select()->toArray();
        if(empty($card)) {
            return [];
        }
        foreach ($card as $value) {
            $discount_cash = $value_cash = $total_cash = $d_car_cash = 0;
            if(0 < $value["cash"] && 0 < $service_price) {
                $true_cash = round($value["cash"] / $value["discount"] * 10, 2);
                if($true_cash < $service_price) {
                    $value_cash += $value["cash"];
                    $service_price -= $true_cash;
                    $discount_cash += $true_cash - $value_cash;
                    $total_cash += $true_cash;
                    $value["cash"] = 0;
                } else {
                    $value_cash += round($service_price * $value["discount"] / 10, 2);
                    $discount_cash += $service_price - $value_cash;
                    $total_cash += $service_price;
                    $value["cash"] -= round($service_price * $value["discount"] / 10, 2);
                    $service_price = 0;
                }
            }
            if(0 < $value["cash"] && 0 < $car_price) {
                $car_discount_cash = $value["cash"] < $car_price ? $value["cash"] : $car_price;
                $car_price -= $car_discount_cash;
                $value_cash += $car_discount_cash;
                $total_cash += $car_discount_cash;
                $d_car_cash += $car_discount_cash;
            }
            if(0 < $total_cash) {
                $list = ["title" => $value["title"], "id" => $value["id"], "discount" => $value["discount"], "discount_cash" => round($discount_cash, 2), "value_cash" => round($value_cash, 2), "total_cash" => round($total_cash, 2), "discount_car_cash" => round($d_car_cash, 2), "true_total_cash" => round($total_cash - $d_car_cash, 2)];
                $arr[] = $list;
            }
        }
        if(0 < $service_price || 0 < $car_price) {
            return [];
        }
        return $arr;
    }
    public function checkChangeUserCard($order_id, $type)
    {
        $share_model = new OrderShare();
        $list = $share_model->where(["order_id" => $order_id, "type" => $type, "status" => 1])->select()->toArray();
        if(!empty($list)) {
            foreach ($list as $value) {
                $cash = $this->where(["id" => $value["card_id"]])->sum("cash");
                if($cash < $value["value_cash"]) {
                    return false;
                }
            }
        }
        return true;
    }
    public function getUserCardList($user_id)
    {
        $dis = ["a.user_id" => $user_id, "a.status" => 1];
        $data = $this->alias("a")->join("massage_balance_discount_order_list b", "a.card_order_id = b.id")->where($dis)->where("a.over_time", ">", time())->where("a.cash", ">", 0)->field("a.*,b.pay_price")->group("a.id")->order("a.discount,a.cash,a.id desc")->paginate(10)->toArray();
        return $data;
    }
    public function updateCardCash($order_id, $user_id, $type)
    {
        $share_model = new OrderShare();
        $water_model = new CardWater();
        $list = $share_model->where(["order_id" => $order_id, "type" => $type, "status" => 1])->select()->toArray();
        if(!empty($list)) {
            foreach ($list as $value) {
                $res = $water_model->updateCash($value["uniacid"], $value["card_id"], $value["value_cash"], -1, $value["order_id"], $type, $user_id);
                if($res == 0) {
                    return false;
                }
                $res = $share_model->dataUpdate(["id" => $value["id"]], ["status" => 2]);
                if($res == 0) {
                    return false;
                }
            }
        }
        return true;
    }
}

?>